/*
 * @Author: zi.yang
 * @Date: 2020-06-18 10:30:12
 * @LastEditTime: 2021-10-10 17:35:38
 * @LastEditors: zi.yang
 * @Description: 用户信息
 * @FilePath: \project-management\models\sys_users.js
 */

const { DataTypes } = require('sequelize');
const moment = require('moment');
const db = require('./db');
const encryption = require('../utils/encryption');
const ep = new encryption();

const config = {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  username: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
    validate: {
      is: {
        args: /^[a-zA-Z]{1}([a-zA-Z0-9]|[._-]){4,19}$/,
        msg: '仅允许5-20位，英文加数字 `.` ,`_`,`-`',
      },
    },
  },
  password: {
    type: DataTypes.STRING,
    allowNull: false,
    set(value) {
      const salt = this.getDataValue('salt');
      const eppwd = ep.$sha1(ep.$md5(value) + salt);
      this.setDataValue('password', eppwd);
    },
  },
  salt: {
    type: DataTypes.STRING,
    set() {
      this.setDataValue('salt', `%%${Math.random().toString(36).substr(2)}%%`);
    },
    defaultValue: Math.random().toString(36).substr(2),
  },
  realname: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
  },
  avatar:{
    type:DataTypes.STRING,
    allowNull: false,
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
    validate: {
      isEmail: true,
    },
  },
  birthday: {
    type: DataTypes.DATE,
    allowNull: true,
    validator:{
      isDate:true
    },
    get() {
      return moment(this.getDataValue('birthday')).format('YYYY-MM-DD');
    },
    set(value) {
      this.setDataValue('birthday', moment(value).format('YYYY-MM-DD'));
    },
  },
  gender: {
    type: DataTypes.STRING,
    validate: {
      isIn: {
        args: [['1', '2']],
        msg: '性别必须是二者之一：`1：男`,`2：女`',
      },
    },
    allowNull: false,
  },
  address: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  phone: {
    type: DataTypes.INTEGER,
    allowNull: false,
    unique: true,
    validate: {
      is: {
        args: /^\d{11}$/,
        msg: '手机号必须是11位数字',
      },
    },
  },
  role_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: '2',
  },
  remark:{
    type:DataTypes.STRING,
    allowNull:true,
    validate:{
      len:[0,80]
    },
    defaultValue:''
  },
  status: {
    type: DataTypes.CHAR,
    allowNull: false,
    validate: { isIn: [[0, 1]] },
    defaultValue: '0',
  },
  deletedAt: {
    field:'delete_time',
    type: DataTypes.DATE,
    allowNull: true,
    get() {
      return moment(this.getDataValue('createdAt')).format(
        'YYYY-MM-DD HH:mm:ss'
      );
    },
    set(value) {
      this.setDataValue(
        'createdAt',
        moment(value).format('YYYY-MM-DD HH:mm:ss')
      );
    },
  },
  createdAt: {
    type: DataTypes.DATE,
    field: 'create_time',
    get() {
      return moment(this.getDataValue('createdAt')).format(
        'YYYY-MM-DD HH:mm:ss'
      );
    },
    set(value) {
      this.setDataValue(
        'createdAt',
        moment(value).format('YYYY-MM-DD HH:mm:ss')
      );
    },
  },
  updatedAt: {
    type: DataTypes.DATE,
    field: 'update_time',
    get() {
      return moment(this.getDataValue('updatedAt')).format(
        'YYYY-MM-DD HH:mm:ss'
      );
    },
    set(value) {
      this.setDataValue(
        'updatedAt',
        moment(value).format('YYYY-MM-DD HH:mm:ss')
      );
    },
  },
};

// 定义模型属性
const Model = db.define('sys_user', config, {
  timestamps: true,
  // 启用偏执表（软删除）
  paranoid: true,
  // 强制表名称等于模型名称
  freezeTableName: true,
});

module.exports = Model;
